package com.netty.demo.text.qotm;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

public class QuoteOfTheMomentClientHandler extends ChannelHandlerAdapter{

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        DatagramPacket data = (DatagramPacket)msg;
        String response = data.content().toString(CharsetUtil.UTF_8);
        if (response.startsWith("QOTM: ")){
            System.out.println("Quote of the Moment: " + response.substring(6));
            ctx.close();
        }
    }
}
